﻿using Omabar.Model;
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using Tom.Net;

namespace Omabar.Entity
{
    public class Slider_M_Entity : EntityBase<SLIDER>
    {
        public override RESP_Info Add(SLIDER model)
        {
            RESP_Info info = new RESP_Info();
            using (SqlConnection conn = new SqlConnection(WebConfigReader.ReadConnectString("OmabarEntities")))
            {
                try
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = "insert into SLIDER_M (SLIDER_ID,SLIDER_IMAGE,SLIDER_URL,SLIDER_DES,SLIDER_SORT,SLIDER_TYPE,SLIDER_BAK) values (@SLIDER_ID,@SLIDER_IMAGE,@SLIDER_URL,@SLIDER_DES,@SLIDER_SORT,@SLIDER_TYPE,@SLIDER_BAK)";
                    cmd.CommandType = System.Data.CommandType.Text;

                    cmd.Parameters.Add(new SqlParameter("SLIDER_ID", model.SLIDER_ID));
                    cmd.Parameters.Add(new SqlParameter("SLIDER_IMAGE", model.SLIDER_IMAGE));
                    cmd.Parameters.Add(new SqlParameter("SLIDER_URL", model.SLIDER_URL));
                    cmd.Parameters.Add(new SqlParameter("SLIDER_DES", model.SLIDER_DES));
                    cmd.Parameters.Add(new SqlParameter("SLIDER_SORT", model.SLIDER_SORT));
                    cmd.Parameters.Add(new SqlParameter("SLIDER_TYPE", model.SLIDER_TYPE));
                    cmd.Parameters.Add(new SqlParameter("SLIDER_BAK", model.SLIDER_BAK));
                    cmd.ExecuteNonQuery();

                    info.Success = true;
                    info.Title = "成功";
                    info.Tip = "<h5><p>添加幻灯片成功</p></h5>";
                    info.Url = "SliderList.aspx";
                    cmd.Dispose();
                }
                catch (Exception ex)
                {
                    info.Success = true;
                    info.Title = "错误";
                    info.Tip = "<h5><p>添加幻灯片失败</p></h5>";
                    info.Url = "SliderAdd.aspx";

                    Tom.Net.Log.LogManager.Error("添加幻灯片失败", ex);
                }
                finally
                {
                    conn.Close();
                }
            }

            return info;
        }

        public override RESP_Info Update(SLIDER model)
        {
            RESP_Info info = new RESP_Info();
            using (SqlConnection conn = new SqlConnection(WebConfigReader.ReadConnectString("OmabarEntities")))
            {
                try
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = "update SLIDER_M set SLIDER_IMAGE=@SLIDER_IMAGE,SLIDER_URL=@SLIDER_URL,SLIDER_DES=@SLIDER_DES,SLIDER_SORT=@SLIDER_SORT,SLIDER_TYPE=@SLIDER_TYPE,SLIDER_BAK=@SLIDER_BAK where SLIDER_ID=@SLIDER_ID";
                    cmd.CommandType = System.Data.CommandType.Text;

                    cmd.Parameters.Add(new SqlParameter("SLIDER_ID", model.SLIDER_ID));
                    cmd.Parameters.Add(new SqlParameter("SLIDER_IMAGE", model.SLIDER_IMAGE));
                    cmd.Parameters.Add(new SqlParameter("SLIDER_URL", model.SLIDER_URL));
                    cmd.Parameters.Add(new SqlParameter("SLIDER_DES", model.SLIDER_DES));
                    cmd.Parameters.Add(new SqlParameter("SLIDER_SORT", model.SLIDER_SORT));
                    cmd.Parameters.Add(new SqlParameter("SLIDER_TYPE", model.SLIDER_TYPE));
                    cmd.Parameters.Add(new SqlParameter("SLIDER_BAK", model.SLIDER_BAK));
                    cmd.ExecuteNonQuery();

                    info.Success = true;
                    info.Title = "编辑";
                    info.Tip = "<h5><p>编辑T图成功</p></h5>";
                    info.Url = "SliderList.aspx?pic_type=" + model.SLIDER_TYPE.ToString();
                    cmd.Dispose();
                }
                catch (Exception ex)
                {
                    info.Success = true;
                    info.Title = "错误";
                    info.Tip = "<h5><p>编辑T图失败</p></h5>";
                    info.Url = "SliderAdd.aspx";

                    Tom.Net.Log.LogManager.Error("编辑T图失败", ex);
                }
                finally
                {
                    conn.Close();
                }
            }

            return info;
        }

        public override RESP_Info Delete(SLIDER slider)
        {
            RESP_Info info = new RESP_Info();
            using (SqlConnection conn = new SqlConnection(WebConfigReader.ReadConnectString("OmabarEntities")))
            {
                try
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = "delete from SLIDER_M where SLIDER_ID=@SLIDER_ID";
                    cmd.CommandType = System.Data.CommandType.Text;

                    cmd.Parameters.Add(new SqlParameter("SLIDER_ID", slider.SLIDER_ID));
                    cmd.ExecuteNonQuery();

                    info.Success = true;
                    info.Title = "删除";
                    info.Tip = "<h5><p>删除幻灯片成功</p></h5>";
                    info.Url = "SliderList.aspx";
                    cmd.Dispose();
                }
                catch (Exception ex)
                {
                    info.Success = true;
                    info.Title = "错误";
                    info.Tip = "<h5><p>删除幻灯片失败</p></h5>";
                    info.Url = "SliderList.aspx";

                    Tom.Net.Log.LogManager.Error("删除幻灯片失败", ex);
                }
                finally
                {
                    conn.Close();
                }
            }

            return info;
        }

        public override SLIDER GetModel(string id)
        {
            using (SqlConnection conn = new SqlConnection(WebConfigReader.ReadConnectString("OmabarEntities")))
            {
                try
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = "select SLIDER_ID,SLIDER_IMAGE,SLIDER_URL,SLIDER_DES,SLIDER_SORT,SLIDER_TYPE,SLIDER_BAK from SLIDER_M where SLIDER_ID=@SLIDER_ID";
                    cmd.CommandType = System.Data.CommandType.Text;

                    cmd.Parameters.Add(new SqlParameter("SLIDER_ID", id));

                    SqlDataReader reader = cmd.ExecuteReader();
                    while (reader.Read())
                    {
                        SLIDER slider = new SLIDER();
                        if (!reader.IsDBNull(0)) slider.SLIDER_ID = reader.GetString(0);
                        if (!reader.IsDBNull(1)) slider.SLIDER_IMAGE = reader.GetString(1);
                        if (!reader.IsDBNull(2)) slider.SLIDER_URL = reader.GetString(2);
                        if (!reader.IsDBNull(3)) slider.SLIDER_DES = reader.GetString(3);
                        if (!reader.IsDBNull(4)) slider.SLIDER_SORT = reader.GetInt16(4);
                        if (!reader.IsDBNull(5)) slider.SLIDER_TYPE = reader.GetInt16(5);
                        if (!reader.IsDBNull(6)) slider.SLIDER_BAK = reader.GetString(6);

                        reader.Close();
                        reader.Dispose();
                        cmd.Dispose();
                        return slider;
                    }
                }
                catch (Exception ex)
                {
                    Tom.Net.Log.LogManager.Error("获取幻灯片数据失败", ex);
                }
                finally
                {
                    conn.Close();
                }
            }
            return null;
        }

        public override SLIDER GetModelBySQL(string sql)
        {
            using (SqlConnection conn = new SqlConnection(WebConfigReader.ReadConnectString("OmabarEntities")))
            {
                try
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = sql;
                    cmd.CommandType = System.Data.CommandType.Text;

                    SqlDataReader reader = cmd.ExecuteReader();
                    while (reader.Read())
                    {
                        SLIDER slider = new SLIDER();
                        if (!reader.IsDBNull(0)) slider.SLIDER_ID = reader.GetString(0);
                        if (!reader.IsDBNull(1)) slider.SLIDER_IMAGE = reader.GetString(1);
                        if (!reader.IsDBNull(2)) slider.SLIDER_URL = reader.GetString(2);
                        if (!reader.IsDBNull(3)) slider.SLIDER_DES = reader.GetString(3);
                        if (!reader.IsDBNull(4)) slider.SLIDER_SORT = reader.GetInt16(4);
                        if (!reader.IsDBNull(5)) slider.SLIDER_TYPE = reader.GetInt16(5);
                        if (!reader.IsDBNull(6)) slider.SLIDER_BAK = reader.GetString(6);

                        reader.Close();
                        reader.Dispose();
                        cmd.Dispose();
                        return slider;
                    }
                }
                catch (Exception ex)
                {
                    Tom.Net.Log.LogManager.Error("获取幻灯片数据失败", ex);
                }
                finally
                {
                    conn.Close();
                }
            }
            return null;
        }

        public override List<SLIDER> GetModels(string sql)
        {
            using (SqlConnection conn = new SqlConnection(WebConfigReader.ReadConnectString("OmabarEntities")))
            {
                try
                {
                    conn.Open();
                    SqlCommand cmd = new SqlCommand();
                    cmd.Connection = conn;
                    cmd.CommandText = sql;
                    cmd.CommandType = System.Data.CommandType.Text;

                    SqlDataReader reader = cmd.ExecuteReader();
                    List<SLIDER> sliders = new List<SLIDER>();
                    while (reader.Read())
                    {
                        SLIDER slider = new SLIDER();
                        if (!reader.IsDBNull(0)) slider.SLIDER_ID = reader.GetString(0);
                        if (!reader.IsDBNull(1)) slider.SLIDER_IMAGE = reader.GetString(1);
                        if (!reader.IsDBNull(2)) slider.SLIDER_URL = reader.GetString(2);
                        if (!reader.IsDBNull(3)) slider.SLIDER_DES = reader.GetString(3);
                        if (!reader.IsDBNull(4)) slider.SLIDER_SORT = reader.GetInt16(4);
                        if (!reader.IsDBNull(5)) slider.SLIDER_TYPE = reader.GetInt16(5);
                        if (!reader.IsDBNull(6)) slider.SLIDER_BAK = reader.GetString(6);

                        sliders.Add(slider);
                    }
                    reader.Close();
                    reader.Dispose();
                    cmd.Dispose();

                    return sliders;
                }
                catch (Exception ex)
                {
                    Tom.Net.Log.LogManager.Error("获取幻灯片数据失败", ex);
                }
                finally
                {
                    conn.Close();
                }
            }

            return null;
        }
    }
}
